package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.pojo.Comment;
import com.itheima.service.CommentService;
import com.itheima.utils.Result;
import com.itheima.utils.StatusCode;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private CommentService commentService;

    /**
     * 增加评论
     *
     * @Param Comment
     */
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment) {
        Integer count = commentService.add(comment);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "插入成功");
        } else {
            return new Result<>(StatusCode.FAIL, "插入失败");
        }
    }

    /**
     * 删除评论
     *
     * @Param Comment
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Comment comment) {
        Integer count = commentService.delete(comment);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "删除成功");
        } else {
            return new Result<>(StatusCode.FAIL, "删除失败");
        }
    }


    /**
     * 批量删除评论
     * @Param List<Comment>
     * */
    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Comment> commentList){
        Integer count = 0;
        if(commentList != null && commentList.size() > 0){
            for (Comment comment : commentList) {
                count = commentService.delete(comment);
            }
        }
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "删除成功");
        } else {
            return new Result<>(StatusCode.FAIL, "删除失败");
        }
    }

    /**
     * 编辑评论
     *
     * @Param Comment
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Comment comment) {
        Integer count = commentService.edit(comment);
        if (count > 0) {
            return new Result<>(StatusCode.SUCCESS, "更新成功");
        } else {
            return new Result<>(StatusCode.FAIL, "更新失败");
        }
    }

    /**
     * 获取评论列表
     *
     * @Param Comment
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Comment comment) {
        Page<Comment> comments = commentService.getList(comment);
        System.out.println(comment);
        Map<String, Object> map = new HashMap<>();
        map.put("records", comments.getResult());
        map.put("total", comments.getTotal());
        map.put("size", comments.getPageSize());
        map.put("current", comments.getPageNum());
        map.put("orders", null);
        map.put("optimizeCountSql", true);
        map.put("isSearchCount", true);
        return new Result<>(StatusCode.SUCCESS, map);
    }

}
